import java.util.Arrays;

public class MyArrayList implements IList {
    public int[] array;
    public int usedSized;
    public static final int DEAFULT_CAPCITY = 7;

    public MyArrayList() {
        this.array = new int[DEAFULT_CAPCITY];
    }

    @Override
    public void add(int data) {
        if (isFull()) {
            grow();
        }
        this.array[usedSized] = data;
        usedSized++;

    }

    private void grow() {
        this.array = Arrays.copyOf(array, 2 * array.length);
    }

    public boolean isFull() {
        return this.usedSized == array.length;
    }

    private void checkPos(int pos) throws PosIllegal {
        if (pos < 0 || pos > usedSized) {
            throw new PosIllegal("pos的位置异常");
        }

    }

    @Override
    public void add(int pos, int data) throws PosIllegal {
        try {
            checkPos(pos);
            if (isFull()) {
                grow();
            }
            for (int i = usedSized - 1; i > pos; i--) {
                array[i] = array[i--];
            }
            array[pos] = data;
            usedSized++;
        } catch (PosIllegal e) {
            System.out.println("不要pos放在奇怪的位置");
            e.printStackTrace();
        }
    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < this.usedSized; i++) {
            if (array[i] == toFind) {
                return true;
            }

        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < this.usedSized; i++) {
            if (array[i] == toFind) {
                System.out.println(array[i]);
                return i;
            }
        }
        return 0;
    }

    private void checkPos2(int pos) throws EmptyException {
        if (pos < 0 || pos > usedSized) {
            throw new EmptyException("pos的位置异常");
        }

    }

    private void checkEmpty() {
        if (isEmpty()) {
            throw new EmptyException("顺序表为空!");
        }

        }
    public boolean isEmpty () {
        return usedSized == 0;
    }
        @Override
        public int get ( int pos){
            try {
                checkEmpty();
                checkPos2(pos);
                return array[pos];
            } catch (EmptyException e) {
                System.out.println("顺序表为空");
                e.printStackTrace();
            }
            return -1;
        }


        @Override
        public void set ( int pos, int value)throws  EmptyException{
        try{
            checkEmpty();
            checkPos2(pos);
             array[pos]  = value;
        }
        catch (EmptyException e) {
        System.out.println("顺序表为空");
        e.printStackTrace();
    }
        }

        @Override
        public void remove ( int toRemove)throws EmptyException,PosIllegal{
               try{
                   checkEmpty();
                   checkPos2(toRemove);
                   for (int i = 0; i <usedSized -1  ; i++) {
                       array[i] = array[i++];
                   }
                   usedSized--;
               }
               catch(PosIllegal E){
                   System.out.println("要移动是空的哦");
               }
               catch (EmptyException e){
            System.out.println("顺序表是空的哦");
        }

        }

        @Override
        public int size () {
            return this.usedSized;
        }

        @Override
        public void clear () {

        usedSized = 0;
        }

        @Override
        public void display () {
            for (int i = 0; i < this.usedSized; i++) {
                System.out.println(array[i]);
            }
        }
    }

